import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import User from '../components/user/user.vue'
import Rights from '../components/power/rights.vue'
import Roles from '../components/power/roles.vue'
import Goods_Cate from '../components/goods/goods_cate.vue'
import Goods_Params from '../components/goods/goods_params.vue'
import Goods_List from '../components/goods/goods_list.vue'
import Goods_Add from '../components/goods/goods_add.vue'
import Orders from '../components/orders/orders.vue'
Goods_Add
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome,
      },
      {
        path: '/users',
        component: User,
      },
      {
        path: '/rights',
        component: Rights,
      },
      {
        path: '/roles',
        component: Roles,
      },
      {
        path: '/categories',
        component: Goods_Cate,
      },
      {
        path: '/params',
        component: Goods_Params,
      },
      {
        path: '/goods',
        component: Goods_List,
      },
      {
        path: '/goods/add',
        component: Goods_Add,
      },
      {
        path: '/orders',
        component: Orders,
      },

    ],

  }
]

const router = new VueRouter({
  routes
})
// 设置路由拦截器
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router
